<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>ed</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_001_014_529">&nbsp;</a>NAME</h4><blockquote>
ed - edit text
</blockquote><h4><a name = "tag_001_014_530">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

ed <b>[</b>-p <i>string</i><b>][</b>-s<b>][</b><i>file</i><b>]</b>

ed <b>[</b>-p <i>string</i><b>][</b>-<b>][</b><i>file</i><b>]</b>
</code>
</pre>
</blockquote><h4><a name = "tag_001_014_531">&nbsp;</a>DESCRIPTION</h4><blockquote>
<br>
The
<i>ed</i>
utility is a line-oriented text editor that uses two modes:
<i>command mode</i>
and
<i>input mode</i>.
In command mode the input characters are interpreted as commands,
and in input mode they are interpreted as text.
See the EXTENDED DESCRIPTION section.
</blockquote><h4><a name = "tag_001_014_532">&nbsp;</a>OPTIONS</h4><blockquote>
The
<i>ed</i>
utility supports the <b>XBD</b> specification, <a href="../xbd/utilconv.html#usg"><b>Utility Syntax Guidelines</b>&nbsp;</a> ,
&nbsp;except for its non-standard usage of "-".
<p>
The following options are supported:
<dl compact>

<dt><b>-p&nbsp;</b><i>string</i>
<dd>
Use
<i>string</i>
as the prompt string when in command mode.
By default, there is no prompt string.

<dt><b>-s</b><dd>Suppress the writing of byte counts by
<b>e</b>,
<b>E</b>,
<b>r</b>
and
<b>w</b>
commands and of the "!" prompt after a
<b>!</b><i>command</i>.

<dt><b>-</b><dd>Same as the <b>-s</b> option.

</dl>
</blockquote><h4><a name = "tag_001_014_533">&nbsp;</a>OPERANDS</h4><blockquote>
The following operand is supported:
<dl compact>

<dt><i>file</i><dd>If the
<i>file</i>
argument is given,
<i>ed</i>
will simulate an
<b>e</b>
command
on the file named by the pathname,
<i>file</i>,
before accepting commands from the standard input.

</dl>
</blockquote><h4><a name = "tag_001_014_534">&nbsp;</a>STDIN</h4><blockquote>
The standard input must be a text file consisting of commands,
as described in the EXTENDED DESCRIPTION section.
</blockquote><h4><a name = "tag_001_014_535">&nbsp;</a>INPUT FILES</h4><blockquote>
The input files must be text files.
</blockquote><h4><a name = "tag_001_014_536">&nbsp;</a>ENVIRONMENT VARIABLES</h4><blockquote>
The following environment variables affect the execution of
<i>ed</i>:
<dl compact>

<dt><i>HOME</i><dd>Determine the pathname of the user's home directory.

<dt><i>LANG</i><dd>Provide a default value for the internationalisation variables
that are unset or null.
If
<i>LANG</i>
is unset or null, the corresponding value from the
implementation-dependent default locale will be used.
If any of the internationalisation variables contains an invalid setting, the
utility will behave as if none of the variables had been defined.

<dt><i>LC_ALL</i><dd>
If set to a non-empty string value,
override the values of all the other internationalisation variables.

<dt><i>LC_COLLATE</i><dd>
Determine the locale for the
behaviour of ranges, equivalence classes
and multi-character collating elements
within regular expressions.


<dt><i>LC_CTYPE</i><dd>
Determine the
locale for the interpretation of sequences of bytes of text data as
characters (for example, single- as opposed to multi-byte characters
in arguments and input files) and
the behaviour of character classes within regular expressions.

<dt><i>LC_MESSAGES</i><dd>
Determine the locale that should be used to affect
the format and contents of diagnostic
messages written to standard error
and informative messages written to standard output.

<dt><i>NLSPATH</i><dd>
Determine the location of message catalogues
for the processing of
<i>LC_MESSAGES .
</i>
</dl>
</blockquote><h4><a name = "tag_001_014_537">&nbsp;</a>ASYNCHRONOUS EVENTS</h4><blockquote>
The
<i>ed</i>
utility will take the
standard action for all signals (see the ASYNCHRONOUS EVENTS section
in
<a href="utildes.html">Utility Description Defaults</a>
<xref href=utildes></xref>)
with the following exceptions:
<dl compact>

<dt>SIGINT<dd>The
<i>ed</i>
utility will interrupt its current activity, write the string
?\n
to standard output, and return to
command mode (see the EXTENDED DESCRIPTION section).

<dt>SIGHUP<dd>
If the buffer is not empty and has changed since the last write,
the
<i>ed</i>
utility will attempt to write a copy of the buffer in a
file.
First, the file named
<b>ed.hup</b>
in the current directory will be
used; if that fails, the file named
<b>ed.hup</b>
in the directory
named by the
<i>HOME</i>
environment variable will be used.
In any case, the
<i>ed</i>
utility will exit without returning to command mode.

</dl>
</blockquote><h4><a name = "tag_001_014_538">&nbsp;</a>STDOUT</h4><blockquote>
Various editing commands and the prompting feature (see
<b>-p</b>)
write to standard output,
as described in the EXTENDED DESCRIPTION section.
</blockquote><h4><a name = "tag_001_014_539">&nbsp;</a>STDERR</h4><blockquote>
Used only for diagnostic messages.
</blockquote><h4><a name = "tag_001_014_540">&nbsp;</a>OUTPUT FILES</h4><blockquote>
The output files are text files whose formats are
dependent on the editing commands given.
</blockquote><h4><a name = "tag_001_014_541">&nbsp;</a>EXTENDED DESCRIPTION</h4><blockquote>
The
<i>ed</i>
utility operates on a copy of the file it is editing; changes made
to the copy will have no effect on the file until a
<b>w</b>
(write)
command is given.
The copy of the text is called the
<i>buffer</i>.
<p>
Commands to
<i>ed</i>
have a simple and regular structure: zero, one or two
<i>addresses</i>
followed by a single-character
<i>command</i>,
possibly followed by parameters to that command.
These addresses specify one or more lines in the buffer.
Every command that requires addresses has default addresses,
so that the addresses very often can be omitted.
If the
<b>-p</b>
option is specified, the prompt string will be written to standard
output before each command is read.
<p>
In general, only one command can appear on a line.
Certain commands allow text to be input.
This text is placed in the appropriate place in the buffer.
While
<i>ed</i>
is accepting text, it is said
to be in <i>input mode</i>.
In this mode, no commands are recognised;
all input is merely collected.
Input mode is
terminated by entering a line consisting of two characters:
a period
(.)
followed by a
newline character.
This line is not considered part of the input text.
<h5><a name = "tag_001_014_541_001">&nbsp;</a>Regular Expressions in ed</h5>
The
<i>ed</i>
utility supports
basic regular expressions, as described in
the <b>XBD</b> specification, <a href="../xbd/re.html#tag_007_003"><b>Basic Regular Expressions</b>&nbsp;</a> .
Since regular expressions in
<i>ed</i>
are always
matched against single lines, never against any larger section of
text, there is no way for a regular expression to match a
newline character.
A null RE is equivalent to the last RE encountered.
<p>
Regular expressions are used in addresses to specify lines, and in some
commands (for example, the
<b>s</b>
substitute command) to specify portions of a line to be
substituted.
<h5><a name = "tag_001_014_541_002">&nbsp;</a>Addresses in ed</h5>
Addressing in
<i>ed</i>
relates to the <i>current line</i>.
Generally, the current line is the last line affected by a command.
The <i>current line number</i> is the address (line number) of
the current line.
The exact effect on the current line number
is discussed under the description of each command.
The
f,
h,
H,
k,
P,
w,
"=" and "!" commands do not modify the current line number.
<p>
Addresses are constructed as follows:
<ol>
<p>
<li>
The character "." (period) addresses the current line.
<p>
<li>
The character "$" addresses the last line of the buffer.
<p>
<li>
A positive decimal number
<i>n</i>
addresses the
<i>n</i>th
line of the buffer.
The first line in the buffer is line number 1.
<p>
<li>
<b>'</b><i>x</i> addresses the line marked with the
mark name character <i>x,</i>
which must be a lower-case letter from the portable character set.
Lines can be marked with the
k
command.
<p>
<li>
An RE enclosed by slashes
(/)
addresses the first line found by searching
forward from the line following the current line toward the end of the
buffer and stopping at the first line containing a string matching the RE.
An address consisting of a null RE delimited by slashes
(//)
addresses the next line containing the last RE encountered.
If necessary, the search will wrap around to the beginning of the buffer
and continue up to and including the current line, so that the entire
buffer is searched.
Within the RE, the sequence
\/
represents a literal
slash instead of the RE delimiter.
<p>
<li>
An RE enclosed in question-marks
(?)
addresses the first line found by searching
backward from the line preceding the current line toward the beginning of
the buffer and stopping at the first line containing a string matching the RE.
If necessary, the search wraps around to the end of the buffer
and continues up to and including the current line.
Within the RE, the sequence
\?
represents a literal question-mark instead of the RE delimiter.
<p>
<li>
An address followed by a plus sign
(+)
or a minus sign
(-)
followed by
a decimal number specifies that address plus
(respectively minus) the indicated number of lines.
The plus sign can be omitted.
<p>
<li>
If an address begins with "+" or "-", the addition or subtraction
is taken with respect to the current line number; for example,
-5
is understood to mean
.-5.
<p>
<li>
If an address ends with "+" or "-",
then 1 will be added to or subtracted from the address, respectively.
As a consequence of this rule and of rule 8 immediately above,
the address "-" refers to the line preceding the current line.
Moreover, trailing "+" and "-" characters have a cumulative effect, so
--
refers to the current line number less 2.
<p>
<li>
A comma
(,)
stands for the address pair
1,$,
while a semicolon
(;)
stands for the pair
.,$.
<p>
</ol>
<p>
Commands require zero, one or two addresses.
Commands that require no addresses regard the presence
of an address as an error.
Commands that accept one or two addresses
assume default addresses when no addresses are given.
If one address is given to a command that allows two addresses,
the command will operate as if it were specified as:
<p><code>
<i>given_address</i>;.
<i>command</i>
</code>
<p>
If more addresses are given than such a command requires,
the results are undefined.
<p>
Typically, addresses are separated from each other by a comma.
They can also be separated by a semicolon.
In the latter case, the current line number
(.)
is set to the first address,
and only then will the second address be calculated.
This feature can be used to determine the starting
line for forward and backward searches (see rules 5 and 6 above).
The second address of any two-address sequence
corresponds to a line that
does not precede,
in the buffer, the line corresponding to the first address.
<h5><a name = "tag_001_014_541_003">&nbsp;</a>Commands in ed</h5>
In the following list of
<i>ed</i>
commands, the default addresses
are shown in parentheses.
The number of addresses shown in the default are the
number expected by the command.
The parentheses are not part of
the address; they show that the given addresses are
the default.
<p>
It is generally invalid for more than one
command to appear on a line.
However, any command
(except
<b>e</b>,
<b>E</b>,
<b>f</b>,
<b>q</b>,
<b>Q</b>,
<b>r</b>,
<b>w</b>
and
<b>!</b>)
can be suffixed by the letter
<b>l</b>,
<b>n</b>
or
<b>p</b>;
in which case, except for the
<b>l</b>,
<b>n</b>
and
<b>p</b>
commands,
the command will be executed and then
the new current line will be written
as described below under the
<b>l</b>,
<b>n</b>
and
<b>p</b>
commands.
When an
<b>l</b>,
<b>n</b>
or
<b>p</b>
suffix is used with an
<b>l</b>,
<b>n</b>
or
<b>p</b>
command, the command will write to
standard output as described below,
but it is unspecified whether the
suffix writes the current line again
in the requested format or whether
the suffix has no effect.
For example, the
<b>pl</b>
command (base
<b>p</b>
command with an
<b>l</b>
suffix) will either write just the
current line or will write it twice  once as specified for
<b>p</b>
and once as specified for
<b>l</b>.
Also, the
<b>g</b>,
<b>G</b>,
<b>v</b>
and
<b>V</b>
commands takes a command as a parameter.
<p>
Each address component can be preceded by zero or more
blank characters.
The command letter can be preceded by zero or more
blank characters.
If a suffix letter
(<b>l</b>,
<b>n</b>
or
<b>p</b>)
is given, it must immediately follow the command.
<p>
The
<b>e</b>,
<b>E</b>,
<b>f</b>,
<b>r</b>
and
<b>w</b>
commands take an optional
<i>file</i>
parameter, separated from the command letter by one or more
blank characters.
<p>
If changes have been made in the buffer since the last
<b>w</b>
command that wrote the entire buffer,
<i>ed</i>
will warn the user if an attempt is made to destroy
the editor buffer via the
<b>e</b>
or
<b>q</b>
commands.
The
<i>ed</i>
utility will write the string:
<pre>
<code>
"?\n"
</code>
</pre>
(followed by an explanatory message if
<i>help mode</i>
has been enabled via the
<b>H</b>
command) to standard output and
will continue in command mode with the current line number unchanged.
If the
<b>e</b>
or
<b>q</b>
command
is repeated with no intervening command, it will take effect.
<p>
If an end-of-file is detected on standard input when a command
is expected, the
<i>ed</i>
utility acts as if a
<b>q</b>
command had been entered.
<p>
If the closing delimiter of an RE or of a
replacement string (for example,
<b>/</b>)
in a
<b>g</b>,
<b>G</b>,
<b>s</b>,
<b>v</b>
or
<b>V</b>
command would be the last character before a newline character,
that delimiter can be omitted, in
which case the addressed line is written.
For example,
the following pairs of commands are equivalent:
<p><code>
<dl compact>

<dt>s/s1/s2<dd>s/s1/s2/p

<dt>g/s1<dd>g/s1/p

<dt>?s1<dd>?s1?

</dl>
</code>
<p>
If an invalid command is entered,
<i>ed</i>
will write the string:
<pre>
<code>
"?\n"
</code>
</pre>
(followed by an explanatory message if
<i>help mode</i>
has been enabled via the
<b>H</b>
command) to standard output and
will continue in command mode with the current line number unchanged.
<h5><a name = "tag_001_014_541_004">&nbsp;</a>Append Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.)a
	&lt;<i>text</i>&gt;
	.
</code>
</pre>
<p>
The
<b>a</b>
command reads the given text
and appends it after the addressed line;
the current line number will become
the address of the last inserted line or, if there
were none, the addressed line.
Address 0 is valid for this command;
it causes the appended text to be placed
at the beginning of the buffer.
<h5><a name = "tag_001_014_541_005">&nbsp;</a>Change Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.,.)c
	&lt;<i>text</i>&gt;
	.
</code>
</pre>
<p>
The
<b>c</b>
command deletes the addressed lines, then accepts input
text that replaces these lines;
the current line will be
set to the address of the last line input; or, if there were none,
at the line after the last line deleted;
if the lines deleted were originally at the end of the buffer,
the current line
number will be set to the address of the new last line; if no lines remain
in the buffer, the current line number will be set to zero.
<h5><a name = "tag_001_014_541_006">&nbsp;</a>Delete Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.,.)d
</code>
</pre>
<p>
The
<b>d</b>
command deletes the addressed lines from the buffer.
The address of the line after the last line deleted will become
the current line number;
if the lines deleted were originally at the end of the buffer,
the current line
number will be set to the address of the new last line; if no lines remain
in the buffer, the current line number will be set to zero.
<h5><a name = "tag_001_014_541_007">&nbsp;</a>Edit Command</h5>
<pre>
<code>
<i>Synopsis</i>:	e <b>[</b><i>file</i><b>]
</b></code>
</pre>
<p>
The
<b>e</b>
command deletes the entire contents of the buffer
and then reads in the file named by the pathname
<i>file</i>.
The current line
number will be set to the address of the last line of the buffer.
If no pathname is given, the currently remembered pathname,
if any, will be used
(see the
<b>f</b>
command).
The number of bytes read will be written to standard output, unless the
<b>-s</b>
option was specified,
in the following format:
<p><code>
<tt>"%d\n"</tt>, &lt;<i>number&nbsp;of&nbsp;bytes&nbsp;read</i>&gt;
</code>
<p>
The name <i>file</i>
will be remembered for possible use as a default pathname
in subsequent
<b>e</b>,
<b>E</b>,
<b>r</b>
and
<b>w</b>
commands.
If
<i>file</i>
is replaced by
<b>!</b>,
the rest of the line
will be taken to be a
shell command line whose output is to be read.
Such a shell command line is not remembered as the current
<i>file</i>.
All marks will be discarded upon the completion of a successful
<b>e</b>
command.
If the buffer has changed since the last time the entire buffer
was written, the user will be warned, as described previously.
<h5><a name = "tag_001_014_541_008">&nbsp;</a>Edit Without Checking Command</h5>
<pre>
<code>
<i>Synopsis</i>:	E <b>[</b><i>file</i><b>]
</b></code>
</pre>
<p>
The
<b>E</b>
command possesses all properties and restrictions of the
<b>e</b>
command except that the editor will not check to see
if any changes have been made to the buffer since the last
w
command.
<h5><a name = "tag_001_014_541_009">&nbsp;</a>Filename Command</h5>
<pre>
<code>
<i>Synopsis</i>:	f <b>[</b><i>file</i><b>]
</b></code>
</pre>
<p>
If
<i>file</i>
is given, the
<b>f</b>
command will change the currently remembered pathname to
<i>file</i>;
whether the name is changed or not,
it then will write the (possibly new) currently remembered pathname
to the standard output
in the following format:
<p><code>
<tt>"%s\n"</tt>, &lt;<i>pathname</i>&gt;
</code>
<p>
The current line number is unchanged.
<h5><a name = "tag_001_014_541_010">&nbsp;</a>Global Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(1,$)g/<i>RE</i>/<i>command list</i>
</code>
</pre>
<p>
In the
<b>g</b>
command, the first step is to mark every line that matches
the given
<i>RE.</i>
Then, for every such line, the given
<i>command list</i> will be executed with
the current line number set to the address of that line.
When the
<b>g</b>
command completes, the current line number will have
the value assigned by the last command in the command list.
If there were no matching lines, the current line number will not be changed.
A single command or the first of a list of commands
will appear on the same line as the global command.
All lines of a multi-line list except the last line will be ended with a
backslash; the
<b>a</b>,
<b>i</b>
and
<b>c</b>
commands and associated input are permitted.
The
.
terminating input mode can be omitted if it would be the
last line of the <i>command list</i>.
An empty <i>command list</i> is equivalent to the
<b>p</b>
command.
The use of the
<b>g</b>,
<b>G</b>,
<b>v</b>,
<b>V</b>
and
<b>!</b>
commands in the
<i>command list</i>
produces undefined results.
Any character other than
space or newline can be used instead of a slash to delimit the
<i>RE</i>.
Within the
<i>RE</i>,
the
<i>RE</i>
delimiter itself can be used as a literal
character if it is preceded by a backslash.
<h5><a name = "tag_001_014_541_011">&nbsp;</a>Interactive Global Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(1,$)G/<i>RE</i>/
</code>
</pre>
<p>
In the
<b>G</b>
command, the first step
is to mark every line that matches the given
<i>RE.</i>
Then, for every such line, that line will be written,
the current line number will be set to the address of that line, and any one
command (other than one of the
<b>a</b>,
<b>c</b>,
<b>i</b>,
<b>g</b>,
<b>G</b>,
<b>v</b>
and
<b>V</b>
commands)
can be input and will be executed.
A
newline character
acts as a null command
(causing no action to be taken on the current line);
an
<b>&amp;</b>
causes the reexecution
of the most recent non-null command executed within
the current invocation of
<b>G</b>.
Note that the commands input as part of the execution of the
<b>G</b>
command can address and affect any lines in the buffer.
The final value of the current line number will be the value set by
the last command successfully executed.
(Note that the last
command successfully executed will be the
<b>G</b>
command itself if a
command fails or the null command is specified.)
If there were
no matching lines, the current line number will not be changed.
The <b>G</b>
command can be terminated by a SIGINT signal.
Any character other than space or newline
can be used instead of a slash to delimit the
<i>RE</i>
and the replacement.
Within the
<i>RE</i>,
the
<i>RE</i>
delimiter itself can be used as a literal
character if it is preceded by a backslash.
<h5><a name = "tag_001_014_541_012">&nbsp;</a>Help Command</h5>
<pre>
<code>
<i>Synopsis</i>:	h
</code>
</pre>
<p>
The
<b>h</b>
command writes a short
message to standard output that explains
the reason for the most recent
<b>?</b>
notification.
The current line number is unchanged.
<h5><a name = "tag_001_014_541_013">&nbsp;</a>Help-mode Command</h5>
<pre>
<code>
<i>Synopsis</i>:	H
</code>
</pre>
<p>
The
<b>H</b>
command causes
<i>ed</i>
to enter a mode in which
help messages (see the
<b>h</b>
command) will be written to standard output for all subsequent
<b>?</b>
notifications.
The
<b>H</b>
command alternatively will turn this mode
on and off; it is initially off.
If the help-mode is being turned on, the
<b>H</b>
command also will explain the previous
<b>?</b>
notification, if there was one.
The current line number is unchanged.
<h5><a name = "tag_001_014_541_014">&nbsp;</a>Insert Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.)i
	&lt;<i>text</i>&gt;
	.
</code>
</pre>
<p>
The
<b>i</b>
command inserts the given text before the addressed line;
.
will be left at the last inserted line or, if there was none,
at the addressed line.
This command differs from the
<b>a</b>
command only in the placement of the input text.
Address 0 is invalid for this command.
<h5><a name = "tag_001_014_541_015">&nbsp;</a>Join Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.,.+1)j
</code>
</pre>
<p>
The
<b>j</b>
command joins contiguous lines by removing the appropriate
newline characters.
If exactly one address is given, this command will do nothing.
If lines are joined, the current line number will be set to the
address of the joined line; otherwise, the current line number
is unchanged.
<h5><a name = "tag_001_014_541_016">&nbsp;</a>Mark Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.)k<i>x</i>
</code>
</pre>
<p>
The
<b>m</b>
command marks the addressed line with name
<i>x</i>,
which must be a lower-case letter
from the portable character set.
The address
'x
then refers to this line;
the current line number
is unchanged.
<h5><a name = "tag_001_014_541_017">&nbsp;</a>List Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.,.)l
</code>
</pre>
<p>
The
<b>l</b>
command writes to standard output the addressed lines in a
visually unambiguous form.
The characters listed in the table in
the <b>XBD</b> specification, <a href="../xbd/notation.html"><b>File Format Notation</b>&nbsp;</a> 
(\\,
\a,
\b,
\f,
\r,
\t,
\v)
will be written as the corresponding escape sequence;
the
\n
in that table is not applicable.
Non-printable characters not in the table
will be written as one three-digit octal number
(with a preceding backslash character)
for each byte in the character (most significant byte first).
If the size of a byte on the system is greater than nine bits,
the format used for non-printable characters is implementation-dependent.
<p>
Long lines will be folded, with the point of
folding indicated by writing
backslash/newline character;
the length at which folding occurs is unspecified,
but should be appropriate for the output device.
The end of each line will be marked with a "$".
An
<b>l</b>
command can be appended to any other command
other than
<b>e</b>,
<b>E</b>,
<b>f</b>,
<b>q</b>,
<b>Q</b>,
<b>r</b>,
<b>w</b>
or
<b>!</b>.
The current line number will be set to the address of the last line written.
<h5><a name = "tag_001_014_541_018">&nbsp;</a>Move Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.,.)m<i>address</i>
</code>
</pre>
<p>
The
<b>m</b>
command repositions the addressed lines after the line
addressed by
<i>address</i>.
Address 0 is valid for
<i>address</i>
and causes the addressed lines to be moved to
the beginning of the buffer.
It is an error if address
<i>address</i>
falls within the range of moved lines.
The current line number will be set to the address of the last line moved.
<h5><a name = "tag_001_014_541_019">&nbsp;</a>Number Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.,.)n
</code>
</pre>
<p>
The
<b>n</b>
command writes to standard output the addressed lines,
preceding each line by its line number and a tab character;
the current line number will be set to the address of the last line written.
The
<b>n</b>
command can be appended to any command other than
<b>e</b>,
<b>E</b>,
<b>f</b>,
<b>q</b>,
<b>Q</b>,
<b>r</b>,
<b>w</b>
or
<b>!</b>.
<h5><a name = "tag_001_014_541_020">&nbsp;</a>Print Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.,.)p
</code>
</pre>
<p>
The
<b>p</b>
command writes to standard output the addressed lines;
the current line number will be set to the address of the last line written.
The
<b>p</b>
command can be appended to any command other than
<b>e</b>,
<b>E</b>,
<b>f</b>,
<b>q</b>,
<b>Q</b>,
<b>r</b>,
<b>w</b>
or
<b>!</b>.
<h5><a name = "tag_001_014_541_021">&nbsp;</a>Prompt Command</h5>
<pre>
<code>
<i>Synopsis</i>:	P
</code>
</pre>
<p>
The
<b>P</b>
command causes
<i>ed</i>
to prompt with an asterisk
(*)
(or
<i>string</i>,
if
<b>-p</b>
is specified)
for all subsequent commands.
The
<b>P</b>
command alternatively turns this mode on and off; it is
initially on if the
<b>-p</b>
option is specified, otherwise off.
The current line number is unchanged.
<h5><a name = "tag_001_014_541_022">&nbsp;</a>Quit Command</h5>
<pre>
<code>
<i>Synopsis</i>:	q
</code>
</pre>
<p>
The
<b>q</b>
command causes
<i>ed</i>
to exit.
If the buffer has changed since the last time the entire buffer
was written, the user will be warned, as described previously.
<h5><a name = "tag_001_014_541_023">&nbsp;</a>Quit Without Checking Command</h5>
<pre>
<code>
<i>Synopsis</i>:	Q
</code>
</pre>
<p>
The
<b>Q</b>
command causes
<i>ed</i>
to exit without
checking if changes have been made in the buffer since the last
<b>w</b>
command.
<h5><a name = "tag_001_014_541_024">&nbsp;</a>Read Command</h5>
<pre>
<code>
<i>Synopsis</i>:	($)r<b>[</b><i>file</i><b>]</b>
</code>
</pre>
<p>
<br>
The
<b>r</b>
command reads in the file named by the pathname
<i>file</i>
and appends it after the addressed line.
If no
<i>file</i>
argument is given, the currently remembered pathname,
if any, will be used
(see
<b>e</b>
and
<b>f</b>
commands).
The currently remembered pathname will not be changed unless
there is no remembered pathname.
Address 0 is valid for
<b>r</b>
and causes the file to be read at the beginning of the buffer.
If the read is successful, and
<b>-s</b>
was not specified, the number of bytes
read will be written to standard output
in the following format:
<p><code>
<tt>"%d\n"</tt>, &lt;<i>number&nbsp;of&nbsp;bytes&nbsp;read</i>&gt;
</code>
The current line number will be set to the address of the last line read in.
If
<i>file</i>
is replaced by
<b>!</b>,
the rest of the line
will be taken to be a
shell command line whose output is to be read.
Such a shell command line will not be remembered as the current pathname.
<h5><a name = "tag_001_014_541_025">&nbsp;</a>Substitute Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.,.)s/<i>RE</i>/<i>replacement</i>/<i>flags</i>
</code>
</pre>
<p>
The
<b>s</b>
command searches each addressed
line for an occurrence of the specified RE
and replace either the first or
all (non-overlapped) matched strings with the
<i>replacement</i>;
see the following description of the
<b>g</b>
suffix.
It is an error if the substitution fails on every addressed line.
Any character other than space or newline
can be used instead of a slash to delimit the
<i>RE</i>
and the replacement.
Within the
<i>RE</i>,
the
<i>RE</i>
delimiter itself can be used as a literal
character if it is preceded by a backslash.
The current line will be set to the address of the last line
on which a substitution occurred.
<p>
An ampersand
(&amp;)
appearing in the
<i>replacement</i>
will be replaced by the string matching the RE on the current line.
The special meaning of "&amp;"
in this context can be
suppressed by preceding it by backslash.
As a more general feature,
the characters
<b>\</b><i>n</i>,
where
<i>n</i>
is a digit,
will be replaced by the text matched by the
corresponding back-reference expression.
When the character
"%"
is the only character
in the
<i>replacement</i>,
the
<i>replacement</i>
used in the most recent substitute
command will be used as the
<i>replacement</i>
in the current substitute command;
if there was no previous substitute command,
the use of
"%"
in this manner is an error.
The
"%"
loses its special meaning when it is
in a replacement string of more than one
character or is preceded by a backslash.
For each backslash (\) encountered in scanning
<i>replacement</i>
from beginning to end, the following character loses its special meaning
(if any).
It is unspecified what special meaning is given to any character
other than "&amp;", "\" "%" or digits.
<p>
A line can be split by substituting a newline character into it.
The application must escape the newline character in the
<i>replacement</i>
by preceding it by backslash.
Such substitution cannot be done as part of a
<b>g</b> or <b>v</b> command list.
The current line number will be set to the address of the last line
on which a substitution is performed.
If no substitution is
performed, the current line number is unchanged.
If a line is
split, a substitution is considered to have been performed on
each of the new lines for the purpose of determining the new
current line number.
A substitution is considered to have been
performed even if the replacement string is identical to the
string that it replaces.
<p>
The value of
<i>flags</i>
must be zero or more of:
<dl compact>

<dt><i>count</i><dd>Substitute for the
<i>count</i>th
occurrence only of the
<i>RE</i>
found on each addressed line.

<dt><b>g</b><dd>Globally substitute for all non-overlapping instances of the
<i>RE</i>
rather than just the first one.
If both
<b>g</b>
and
<i>count</i>
are specified,
the results are unspecified.

<dt><b>l</b><dd>Write to standard output
the final line in which a substitution was made.
The line will be written
in the format specified for the
<b>l</b>
command.

<dt><b>n</b><dd>Write to standard output
the final line in which a substitution was made.
The line will be written
in the format specified for the
<b>n</b>
command.

<dt><b>p</b><dd>Write to standard output
the final line in which a substitution was made.
The line will be written
in the format specified for the
<b>p</b>
command.

</dl>
<h5><a name = "tag_001_014_541_026">&nbsp;</a>Copy Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.,.)t<i>address</i>
</code>
</pre>
<p>
The
<b>t</b>
command is equivalent to the
<b>m</b>
command, except that a copy of the addressed lines will be placed
after address
<i>address</i>
(which can be 0);
the current line number will be set to the address of the last line added.
<h5><a name = "tag_001_014_541_027">&nbsp;</a>Undo Command</h5>
<pre>
<code>
<i>Synopsis</i>:	u
</code>
</pre>
<p>
The
<b>u</b>
command nullifies the effect of the most recent
command that modified anything in the buffer, namely
the most recent
<b>a</b>,
<b>c</b>,
<b>d</b>,
<b>g</b>,
<b>i</b>,
<b>j</b>,
<b>m</b>,
<b>r</b>,
<b>s</b>,
<b>t</b>,
<b>u</b>,
<b>v</b>,
<b>G</b>
or
<b>V</b>
command.
All changes made to the buffer by a
<b>g</b>,
<b>G</b>,
<b>v</b>
or
<b>V</b>
global command will be undone as a single change;
if no changes were made by the global command (such as with
<b>g/</b>RE
the
<b>u</b>
command will have no effect.
The current line number will be set to the value it had
immediately before the
command being undone started.
<h5><a name = "tag_001_014_541_028">&nbsp;</a>Global Non-matched Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(1,$)v/<i>RE</i>/<i>command list</i>
</code>
</pre>
<p>
This command is equivalent to the global command
<b>g</b>
except that the
lines that are marked during the first step will be those that do
not match the
<i>RE</i>.
<h5><a name = "tag_001_014_541_029">&nbsp;</a>Interactive Global Not-matched Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(1,$)V/<i>RE</i>/
</code>
</pre>
<p>
This command is equivalent to the interactive global command
<b>G</b> except
that the lines that are marked during the first step will be those that do
not match the
<i>RE</i>.
<h5><a name = "tag_001_014_541_030">&nbsp;</a>Write Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(1,$)w<b>[</b><i>file</i><b>]
</b></code>
</pre>
<p>
The
<b>w</b>
command writes the addressed lines into
the file named by the pathname
<i>file</i>.
The command will create the file, if it does not exist, or
will replace the contents of the existing file.
The currently remembered pathname will not be changed unless
there is no remembered pathname.
If no pathname is given, the currently remembered pathname, if any,
will be used (see the
<b>e</b>
and
<b>f</b>
commands); the current line number is unchanged.
If the command is successful, the number of bytes written will be
written to standard output, unless the
<b>-s</b>
option was specified, in the following format:
<p><code>
<tt>"%d\n"</tt>, &lt;<i>number&nbsp;of&nbsp;bytes&nbsp;written</i>&gt;
</code>
<p>
If
<i>file</i>
begins with "!", the rest of the line will be taken to be a
shell command line whose standard input will be the addressed lines.
Such a shell command line will not be remembered as the current pathname.
This usage of the write command with "!" will not be considered as a &quot;last
<b>w</b>
command that wrote the entire buffer&quot;, as described previously;
thus, this alone will not prevent
the warning to the user if an attempt is made to destroy
the editor buffer via the
<b>e</b>
or
<b>q</b>
commands.
<h5><a name = "tag_001_014_541_031">&nbsp;</a>Line Number Command</h5>
<pre>
<code>
<i>Synopsis</i>:	($)=
</code>
</pre>
<p>
The line number of the addressed line will be
written to standard output
in the following format:
<p><code>
<tt>"%d\n"</tt>, &lt;<i>line&nbsp;number</i>&gt;
</code>
<p>
The current line number is unchanged by this command.
<h5><a name = "tag_001_014_541_032">&nbsp;</a>Shell Escape Command</h5>
<pre>
<code>
<i>Synopsis</i>:	!<i>command</i>
</code>
</pre>
<p>
The remainder of the line after the
<b>!</b>
will be sent
to the command interpreter
to be interpreted as a shell command line.
Within the text of that shell command line, the unescaped character
<b>%</b>
will be replaced with the remembered pathname;
if a
<b>!</b>
appears as the first character of the command,
it will be replaced with the text of the previous shell command
executed via
<b>!</b>.
Thus,
<b>!!</b>
will repeat the previous
<b>!</b>command
If any replacements of
<b>%</b>
or
<b>!</b>
are performed, the modified line will be written
to the standard output before
<i>command</i>
is executed.
The
<b>!</b>
command will write:
<pre>
<code>
"!\n"
</code>
</pre>
to standard output upon completion, unless the
<b>-s</b>
option is specified.
The current line number is unchanged.
<h5><a name = "tag_001_014_541_033">&nbsp;</a>Null Command</h5>
<pre>
<code>
<i>Synopsis</i>:	(.+1)
</code>
</pre>
<p>
An address alone on a line causes the addressed line to be written.
A newline character alone is equivalent to
.+1p.
The current line number will be set to the address of the written line.
</blockquote><h4><a name = "tag_001_014_542">&nbsp;</a>EXIT STATUS</h4><blockquote>
The following exit values are returned:
<dl compact>

<dt>0<dd>Successful completion without any file or command errors.

<dt>&gt;0<dd>An error occurred.

</dl>
</blockquote><h4><a name = "tag_001_014_543">&nbsp;</a>CONSEQUENCES OF ERRORS</h4><blockquote>
When an error in the input script is encountered, or when
an error is detected that is a consequence of the data
(not) present in the file or due to an external condition
such as a read or write error:
<ul>
<p>
<li>
If the standard input is a terminal device file,
all input will be flushed, and a new command read.
<p>
<li>
If the standard input is a regular file,
<i>ed</i>
will terminate with a non-zero exit status.
<p>
</ul>
</blockquote><h4><a name = "tag_001_014_544">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
Because of the extremely terse nature of the default error
messages, the prudent script writer
will begin the
<i>ed</i>
input commands with an
<b>H</b>
command, so that if any errors do occur at
least some clue as to the cause will be made available.
</blockquote><h4><a name = "tag_001_014_545">&nbsp;</a>EXAMPLES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_001_014_546">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
The obsolescent single-minus form may be withdrawn in a future issue.
Applications should use the
<b>-s</b>
option.
<p>
The IEEE PASC 1003.2 Interpretations Committee has forwarded concerns about 
parts of this interface definition to the IEEE PASC Shell and Utilities Working Group
which is identifying the corrections.
A future revision of this specification will align with
IEEE Std. 1003.2b when finalised.
</blockquote><h4><a name = "tag_001_014_547">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="ex.html">ex</a></i>,
<i><a href="sed.html">sed</a></i>,
<i><a href="sh.html">sh</a></i>,
<i><a href="vi.html">vi</a></i>.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>
